<?php 

/**
 * 分类控制器
 */
class CategoryController extends AuthController{
    /**
     * 后台分类
     */
    public function index(){
    	$cate=M()->query("SELECT * FROM hd_category");
		//p($cate);die;
		$this->assign('cate',$cate);	
        $this->display(); 
    }
	/**
	 * 获得顶级分类
	 */
	public function addTop(){
		if(IS_POST){
			//找到title声明
			$title = $_POST['title'];
			//输出分类的标题
			$sql = "INSERT INTO hd_category SET title='{$title}'";
			//插入一条sql语句
			M()->exec($sql);
			$this->success('添加顶级分类成功',U('index'));
		}
		
		$this->display();
	}
	/*
	 * 获得子分类
	 * */
	public function addSon(){

			if(IS_POST){
				$title=$_POST['title'];
				$pid=$_POST['pid'];
				//添加子分类就是设置pid过程
				$sql="INSERT INTO hd_category SET title='{$title}',pid={$pid}";
				M()->exec($sql);
			    $this->success('添加成功',U('index'));
		}
		$this->display();
	}
	/*
	 * 修改
	 * */
	public function edit(){
		
		$cid = (int)$_GET['cid'];
		
		if(IS_POST){
			$title=$_POST['title'];
			//执行修改
			$sql="UPDATE hd_category SET title='{$title}'WHERE cid={$cid}";
			M()->exec($sql);
			
			$this->success('修改成功',U('index'));
		}
		//获得旧内容
		$oldData = M()->query("SELECT * FROM hd_category WHERE cid={$cid}");
		$this->assign('oldData',$oldData[0]);
		
		$this->display();
	}
	
	/*
	 * 删除
	 * */
	 public function del(){
	 	
		$cid=isset($_GET['cid']) ? (int)$_GET['cid'] : 0;
		//p($cid);
		M()->exec("DELETE FROM hd_category WHERE cid={$cid}");
		$this->success('删除成功',U('index'));
	
		
		
	 }
	
	}

 ?>
 
 
 
 
 
 
 
 
 
